package com.socialsoftware.Page;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.core.view.WindowCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.socialsoftware.R;
import com.socialsoftware.MediaCommunication.activity.AudioChatActivity;
import com.socialsoftware.MediaCommunication.activity.ScreenSharingActivity;
import com.socialsoftware.MediaCommunication.activity.VideoChatActivity;
import com.socialsoftware.MediaCommunication.model.CallMode;
import com.socialsoftware.MediaCommunication.utils.Constants;
import com.socialsoftware.repository.ContactRepository;
public class ChatDetailActivity extends AppCompatActivity {
    private static final String TAG = "ChatDetailActivity";
    private Toolbar toolbar;
    private TextView tvTitle;
    private RecyclerView recyclerView;
    private EditText etMessage;
    private Button btnSend;
    private ImageButton btnVoice, btnEmoji, btnShowFunctions;
    private ImageView btnCamera, btnGallery, btnFileShare, btnLocation;
    private ImageView btnVoiceCall, btnVideoCall, btnScreenShare, btnMore;
    private LinearLayout functionLayout;
    private FrameLayout voiceInputLayout;
    private String chatId;
    private String chatName;
    private String chatUsername;
    private boolean isShowingFunctionLayout = false;
    private ContactRepository contactRepository;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupStatusBar();
        setContentView(R.layout.activity_chat_detail);
        contactRepository = new ContactRepository(getApplication());
        if (getIntent() != null) {
            chatId = getIntent().getStringExtra("chat_id");
            chatName = getIntent().getStringExtra("chat_name");
            chatUsername = getIntent().getStringExtra("chat_username");
            if (chatUsername == null) {
                chatUsername = chatName;
                Log.d(TAG, "Username not provided, using chatName as username: " + chatUsername);
            }
        }
        initViews();
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
        }
        tvTitle.setText(chatName != null ? chatName : getString(R.string.message_detail));
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        setupClickListeners();
        setupTextWatcher();
    }
    private void setupStatusBar() {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(ContextCompat.getColor(this, R.color.purple_700));
        WindowCompat.setDecorFitsSystemWindows(window, true);
    }
    private void initViews() {
        toolbar = findViewById(R.id.toolbar);
        tvTitle = findViewById(R.id.tvTitle);
        recyclerView = findViewById(R.id.recyclerView);
        etMessage = findViewById(R.id.etMessage);
        btnSend = findViewById(R.id.btnSend);
        btnVoice = findViewById(R.id.btnVoice);
        btnEmoji = findViewById(R.id.btnEmoji);
        btnShowFunctions = findViewById(R.id.btnShowFunctions);
        btnCamera = findViewById(R.id.btnCamera);
        btnGallery = findViewById(R.id.btnGallery);
        btnFileShare = findViewById(R.id.btnFileShare);
        btnLocation = findViewById(R.id.btnLocation);
        btnVoiceCall = findViewById(R.id.btnVoiceCall);
        btnVideoCall = findViewById(R.id.btnVideoCall);
        btnScreenShare = findViewById(R.id.btnScreenShare);
        btnMore = findViewById(R.id.btnMore);
        functionLayout = findViewById(R.id.functionLayout);
        voiceInputLayout = findViewById(R.id.voiceInputLayout);
    }
    private void setupTextWatcher() {
        etMessage.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (TextUtils.isEmpty(s)) {
                    btnSend.setVisibility(View.GONE);
                    btnShowFunctions.setVisibility(View.VISIBLE);
                } else {
                    btnSend.setVisibility(View.VISIBLE);
                    btnShowFunctions.setVisibility(View.GONE);
                }
            }
            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }
    private void setupClickListeners() {
        btnSend.setOnClickListener(v -> {
            String message = etMessage.getText().toString().trim();
            if (!message.isEmpty()) {
                // In a real app, you would send the message to the server
                Toast.makeText(ChatDetailActivity.this, "发送: " + message, Toast.LENGTH_SHORT).show();
                etMessage.setText("");
            }
        });
        btnVoice.setOnClickListener(v -> {
            Toast.makeText(ChatDetailActivity.this, "长按录音", Toast.LENGTH_SHORT).show();
        });
        btnVoice.setOnTouchListener((v, event) -> {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    voiceInputLayout.setVisibility(View.VISIBLE);
                    return true;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    voiceInputLayout.setVisibility(View.GONE);
                    Toast.makeText(ChatDetailActivity.this, "语音消息已发送", Toast.LENGTH_SHORT).show();
                    return true;
            }
            return false;
        });
        btnEmoji.setOnClickListener(v -> {
            Toast.makeText(ChatDetailActivity.this, "表情功能开发中", Toast.LENGTH_SHORT).show();
        });
        btnShowFunctions.setOnClickListener(v -> {
            toggleFunctionLayout();
        });
        btnCamera.setOnClickListener(v -> {
        });
        btnGallery.setOnClickListener(v -> {
            Toast.makeText(ChatDetailActivity.this, "相册功能开发中", Toast.LENGTH_SHORT).show();
            toggleFunctionLayout();
        });
        btnFileShare.setOnClickListener(v -> {
            Toast.makeText(ChatDetailActivity.this, "文件功能开发中", Toast.LENGTH_SHORT).show();
            toggleFunctionLayout();
        });
        btnLocation.setOnClickListener(v -> {
            Toast.makeText(ChatDetailActivity.this, "位置功能开发中", Toast.LENGTH_SHORT).show();
            toggleFunctionLayout();
        });
        btnVoiceCall.setOnClickListener(v -> {
            Intent intent = new Intent(ChatDetailActivity.this, 
                AudioChatActivity.class);
            intent.putExtra("contact_username", chatUsername);
            intent.putExtra("contact_name", chatName);
            intent.putExtra(Constants.EXTRA_CALL_MODE, CallMode.AUDIO.getValue());
            Log.d(TAG, "发起语音通话，目标用户: " + chatUsername);
            startActivity(intent);
            toggleFunctionLayout();
        });
        btnVideoCall.setOnClickListener(v -> {
            Intent intent = new Intent(ChatDetailActivity.this, 
                VideoChatActivity.class);
            intent.putExtra("contact_username", chatUsername);
            intent.putExtra("contact_name", chatName);
            intent.putExtra(Constants.EXTRA_CALL_MODE, CallMode.VIDEO.getValue());
            Log.d(TAG, "发起视频通话，目标用户: " + chatUsername);
            startActivity(intent);
            toggleFunctionLayout();
        });
        btnScreenShare.setOnClickListener(v -> {
            Intent intent = new Intent(ChatDetailActivity.this, 
                ScreenSharingActivity.class);
            intent.putExtra("contact_username", chatUsername);
            intent.putExtra("contact_name", chatName);
            intent.putExtra(Constants.EXTRA_CALL_MODE, CallMode.SCREEN.getValue());
            Log.d(TAG, "发起屏幕共享，目标用户: " + chatUsername);
            startActivity(intent);
            toggleFunctionLayout();
        });
        btnMore.setOnClickListener(v -> {
            Toast.makeText(ChatDetailActivity.this, "更多功能开发中", Toast.LENGTH_SHORT).show();
            toggleFunctionLayout();
        });
    }
    private void toggleFunctionLayout() {
        isShowingFunctionLayout = !isShowingFunctionLayout;
        functionLayout.setVisibility(isShowingFunctionLayout ? View.VISIBLE : View.GONE);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.chat_detail_menu, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        } else if (item.getItemId() == R.id.action_chat_info) {
            Intent intent = new Intent(ChatDetailActivity.this, ChatInfoActivity.class);
            intent.putExtra("chat_id", chatId);
            intent.putExtra("chat_name", chatName);
            startActivity(intent);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 